# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=abinit
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=10.0.5
pkgrel=1
pkgdesc="Atomic-scale simulation software. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='https://www.abinit.org'
msys2_repository_url="https://github.com/abinit/abinit"
msys2_references=(
  'aur: abinit'
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-libpsml"
         "${MINGW_PACKAGE_PREFIX}-libxc"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-netcdf-fortran"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-msmpi"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-wannier90")
source=("https://www.abinit.org/sites/default/files/packages/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-on-mingw.patch"
        "002-fix-pkgconfig-file.patch"
        "003-fix-version.patch")
sha256sums=('07fed4df03ae32178933373b990bbda4431ea836fc7bebec05b17e4267bb7f4e'
            'eb4db135a0d01a0e5624aca081817b6a66ef433933331d0bd89e3c9ee8fd2b18'
            '466f7f86384f02c99e9f7a948f2b38229a10962ff849cd1e8bf06b0c6ddd7481'
            'c17be9dc3e3b4f2dd84fbb4133daba4a8ac84240aea487ee0790850fc7e51538')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/001-fix-build-on-mingw.patch
  patch -Np1 -i "${srcdir}"/002-fix-pkgconfig-file.patch
  patch -Np1 -i "${srcdir}"/003-fix-version.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DPYMOD_INSTALL_LIBDIR=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBLA_VENDOR=OpenBLAS \
      -DWANNIER_ROOT=${MINGW_PREFIX} \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
